// app.js
var util = require('./utils/util.js')
App({
  globalData: {
    userInfo: null,
    extraData: null,
    getGlobalShareMedia: null
  },
  onLaunch(options) {
    // 初始化云开发环境
    wx.cloud.init({
      env: 'main-9g11cspaf548a298'
    })
    
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    })
    this.getGlobalShareMedia()
    this.selecteUser()
  },
  onShow(options) {
    if (options.referrerInfo.extraData && options.referrerInfo.extraData.tixian_pingtai) {
      this.globalData.extraData = options.referrerInfo.extraData
    }
    if (options.referrerInfo.extraData && options.referrerInfo.extraData.pingtai) {
      this.globalData.extraData = options.referrerInfo.extraData
    }
    // 访问次数方法
    this.getFangwen()
  },

  // 访问次数方法
  getFangwen() {
    wx.cloud.callFunction({
      name: 'fangwen',
      data: {
        tag: 'fangwencishu',
        _updateTime: util.formatTime(new Date()).slice(0, 10)
      }
    })
  },


  // 查询用户状态
  selecteUser() {
    return new Promise((resolve, reject) => {
      wx.cloud.callFunction({
          name: 'user',
          data: {
            tag: 'selecteUser',
            _updateTime: util.formatTime(new Date()).slice(0, 10)
          }
        })
        .then(res => {
          if (res.result.length === 1) {
            // 1.用户已经注册，判断禁用状态  为true 则保存注册状态  为false 则叫他滚蛋
            if (res.result[0].status) {
              this.globalData.userInfo = res.result[0]
              resolve(this.globalData.userInfo)
            } else {
              resolve(false)
              this.jingyongShow()
            }
          } else {
            resolve('Not_registered')
          }
        })
    })
  },

  // 异步获取全局媒体资源及分享转发资源图片及文字
  getGlobalShareMedia() {
    let that = this;
    return new Promise((resolve, reject) => {
      wx.cloud.database().collection('jinbi_page_top_show').get()
        .then(res => {
          that.globalData.getGlobalShareMedia = res.data[0]
          resolve(that.globalData.getGlobalShareMedia)
        })
    })
  },
  // 定义检查用户禁用状态
  jingyongShow() {
    wx.showModal({
      showCancel: false,
      title: '你的账号涉嫌违规',
      content: '请联系管理员处理!',
      cancelColor: 'cancelColor',
      complete: () => {
        wx.exitMiniProgram()
      }
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})